/*
 * Copyright (c) 2011.
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.graphoman.gui;

import org.graphoman.Action;
import org.graphoman.model.Model;
import org.graphoman.model.State;
import org.graphoman.undo.CreateAction;
import org.graphoman.undo.DeleteAction;
import org.graphoman.undo.UndoAction;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: daniil Date: 11.10.11
 * Time: 10:36
 */
public class StateDialog extends JFrame {
    /**
     * for creating new object
     *
     * @param action
     * @param model
     */
    JTextField nameField;
    JTextField idField;
    JTextField xTextField;
    JTextField yTextField;
    JTextField sizeField;

    JButton saveButton;
    JButton deleteButton;

    public static void showEditDialog(Action action, Model model, State state, List<UndoAction> undoActionList) {
        final StateDialog dialog = new StateDialog(action, model, state, undoActionList);
        dialog.setVisible(true);
    }

    public static void showCreateDialog(Action action, final Model model, final List<UndoAction> undoActionList) {
        final State state = new State();
        final StateDialog dialog = new StateDialog(action, model, state, undoActionList);
        dialog.getDeleteButton().setVisible(false);
        dialog.getSaveButton().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                model.states.add(state);
                undoActionList.add(new CreateAction(state));
            }
        });
        dialog.setVisible(true);

    }

    public StateDialog(final Action action, final Model model, final State state, final List<UndoAction> undoActionList) {
        setLayout(new BorderLayout());
        JPanel contentPanel = new JPanel(new GridLayout(6, 2, 20, 20));
        contentPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        JLabel nameLabel = new JLabel("name");
        nameField = new JTextField();
        contentPanel.add(nameLabel);
        contentPanel.add(nameField);
        nameField.setText(state.name);

        final JLabel idLabel = new JLabel("id");
        idField = new JTextField();
        contentPanel.add(idLabel);
        contentPanel.add(idField);
        idField.setText(state.id);

        JLabel xLabel = new JLabel("x");
        xTextField = new JTextField();
        contentPanel.add(xLabel);
        contentPanel.add(xTextField);
        xTextField.setText(String.valueOf(state.x));

        JLabel yLabel = new JLabel("y");
        yTextField = new JTextField();
        contentPanel.add(yLabel);
        contentPanel.add(yTextField);
        yTextField.setText(String.valueOf(state.y));

        JLabel sizeLabel = new JLabel("size");
        sizeField = new JTextField();
        contentPanel.add(sizeLabel);
        contentPanel.add(sizeField);
        sizeField.setText(String.valueOf(state.diameter));


        JLabel colorLabel = new JLabel("color");
        contentPanel.add(colorLabel);
        final JPanel colorChoosePanel = new JPanel();
        colorChoosePanel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                Color color = JColorChooser.showDialog(null, "choose color", state.color);
                if (color != null) {
                    colorChoosePanel.setBackground(color);
                    state.color = color;
                }
            }
        });
        colorChoosePanel.setBackground(state.color);
        contentPanel.add(colorChoosePanel);


        // buttons
        JPanel buttonPanel = new JPanel();
        saveButton = new JButton("save");
        saveButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent actionEvent) {
                if (!idField.getText().isEmpty()) {
                    state.name = nameField.getText();
                    state.id = idField.getText();
                    state.x = Integer.parseInt(xTextField.getText());
                    state.y = Integer.parseInt(yTextField.getText());
                    state.diameter = Integer.parseInt(sizeField.getText());
                    if (action != null) {
                        // repaint
                        action.perform();
                    }
                    setVisible(false);
                } else {
                    idLabel.setForeground(Color.red);
                }
            }

        });

        JButton cancelButton = new JButton("cancel");
        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                setVisible(false);
            }
        });

        deleteButton = new JButton("delete");
        deleteButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                model.states.remove(state);
                DeleteAction deleteAction = new DeleteAction(state);
                deleteAction.transitions.addAll(model.getConnectedTransitions(state));
                model.transitions.removeAll(model.getConnectedTransitions(state));
                undoActionList.add(deleteAction);
                setVisible(false);
                action.perform();
            }
        });

        buttonPanel.add(saveButton);
        buttonPanel.add(cancelButton);
        buttonPanel.add(deleteButton);

        add(contentPanel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);

        pack();
        setLocationRelativeTo(null);
    }

    public JButton getDeleteButton() {
        return deleteButton;
    }

    public JButton getSaveButton() {
        return saveButton;
    }

}
